package edu.hawaii.myisern.action;

import java.util.ArrayList;
import java.util.List;
import edu.hawaii.myisern.model.Log;
import edu.hawaii.myisern.model.Model;
import edu.hawaii.myisern.model.MyOrganization;
import edu.hawaii.myisern.model.MyResearcher;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.action.DontValidate;

/**
 * Action bean for new researcher.
 * 
 * @author Randy Cox
 */
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public class EditResearcherActionBean extends MyIsernActionBean {

  @Validate(required = true)
  private String name;
  private String email;
  private String organization;
  private String pictureLink;
  private String bioStatement;
  private List<String> organizations = new ArrayList<String>();
  private String importFile;
  private String exportFile;

  /**
   * Gets name.
   * 
   * @return name of researcher.
   */
  public String getName() {
    return name;
  }

  /**
   * Sets name.
   * 
   * @param name of researcher.
   */
  public void setName(String name) {
    this.name = name;
  }

  /**
   * Gets organizations.
   * 
   * @return the organizations
   */
  public List<String> getOrganizations() {
    return organizations;
  }

  /**
   * Get Email of researcher.
   * 
   * @return Email of researcher.
   */
  public String getEmail() {
    return email;
  }

  /**
   * Set Email of researcher.
   * 
   * @param email or researcher.
   */
  public void setEmail(String email) {
    this.email = email;
  }

  /**
   * @return the organization
   */
  public String getOrganization() {
    return organization;
  }

  /**
   * @param organization the organization to set
   */
  public void setOrganization(String organization) {
    this.organization = organization;
  }

  /**
   * Get Picture link of researcher.
   * 
   * @return picture link of researcher.
   */
  public String getPictureLink() {
    return pictureLink.trim();
  }

  /**
   * Set Picture link of researcher.
   * 
   * @param pictureLink or researcher.
   */
  public void setPictureLink(String pictureLink) {
    this.pictureLink = pictureLink;
  }

  /**
   * Get Bio statement.
   * 
   * @return BioStatement of researcher.
   */
  public String getBioStatement() {
    return bioStatement;
  }

  /**
   * Set bio statement.
   * 
   * @param bioStatement or researcher.
   */
  public void setBioStatement(String bioStatement) {
    this.bioStatement = bioStatement;
  }

  /**
   * @return the importFile
   */
  public String getImportFile() {
    return importFile;
  }

  /**
   * @param importFile the importFile to set
   */
  public void setImportFile(String importFile) {
    this.importFile = importFile;
  }

  /**
   * @return the exportFile
   */
  public String getExportFile() {
    return exportFile;
  }

  /**
   * @param exportFile the exportFile to set
   */
  public void setExportFile(String exportFile) {
    this.exportFile = exportFile;
  }

  /**
   * Loads a record on to the form ready for editing.
   * 
   * @return resolution of next page.
   */
  @DontValidate
  public Resolution preEdit() {
    Model model = Model.getInstance();

    // populate org list
    List<MyOrganization> results = model.listOrganizations();
    Integer count = results.size();
    Log.enter("EditResearcherActionBean.preEdit after model.ListOrganizations, count = "
        + count.toString());
    List<String> list = new ArrayList<String>();
    for (MyOrganization record : results) {
      list.add(record.getName());
    }
    count = list.size();
    Log.enter("EditResearcherActionBean.preEdit after for (List), count = " + count.toString());

    // retrieve data
    getContext().getRequest().getSession().setAttribute("organizations", list);
    MyResearcher researcher = null;
    Log.enter("EditResearcherActionBean.preEdit before getReseacher, name = " + this.name);
    if (this.name != null) {
      researcher = this.model.getResearcher(this.name);
    }
    if (researcher == null) {
      Log.error("EditResearcherActionBean.preEdit unexpected null afeter get researcher.");
      researcher = new MyResearcher();
      researcher.setName(this.name);
    }

    // populate data
    this.name = researcher.getName();
    Log.enter("EditResearcherActionBean.preEdit after getReseacher, name = " + this.name);
    this.email = researcher.getEmail();
    this.organization = researcher.getOrganization();
    this.pictureLink = researcher.getPictureLink();
    this.bioStatement = researcher.getBio();
    return new RedirectResolution("/editResearcher.jsp").flash(this);
  }

  /**
   * Delete record.
   * 
   * @return resolution back to same page.
   */
  public Resolution delete() {
    this.model.deleteResearcher(this.name);
    return new RedirectResolution("/selectResearcher.jsp").flash(this);
  }

  /**
   * Save record.
   * 
   * @return resolution back to same page.
   */
  public Resolution save() {
    MyResearcher researcher = new MyResearcher();
    researcher.setName(this.name);
    researcher.setEmail(this.email);
    researcher.setOrganization(this.organization);
    researcher.setPictureLink(this.pictureLink);
    researcher.setBio(this.bioStatement);
    this.model.saveResearcher(researcher);
    return new RedirectResolution("/selectResearcher.jsp");
  }

  /**
   * Cancel and return to main menu.
   * 
   * @return resolution back to same page.
   */
  @DontValidate
  public Resolution cancel() {
    return new RedirectResolution("/selectResearcher.jsp");
  }

  /**
   * Clear data.
   * 
   * @return resolution back to same page.
   */
  @DontValidate
  public Resolution clearData() {
    Model.getInstance().clearResearchers();
    return new RedirectResolution("/selectResearcher.jsp").flash(this);
  }

  /**
   * Imports xml data.
   * 
   * @return resolution back to same page.
   */
  @DontValidate
  public Resolution importData() {
    Model.getInstance().importResearchers(this.importFile);
    return new RedirectResolution("/selectResearcher.jsp").flash(this);
  }

  /**
   * Exports xml data.
   * 
   * @return resolution back to same page.
   */
  @DontValidate
  public Resolution exportData() {
    Model.getInstance().exportResearchers(this.exportFile);
    return new RedirectResolution("/selectResearcher.jsp").flash(this);
  }

}
